package com.qm.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qm.mapper.UserMapper;
import com.qm.pojo.User;
import com.qm.service.DubboUserService;
import com.qm.util.ObjectMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import redis.clients.jedis.JedisCluster;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.UUID;

/**
 @author james
 @create 2021-04-26 10:36
 */
@Service    //dubbo service注解
public class DubboUserServiceImpl implements DubboUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JedisCluster jedisCluster;

    @Override
    @Transactional
    public void saveUser(User user) {
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password)
            .setEmail(user.getPhone())
            .setCreated(new Date())
            .setUpdated(new Date());
        userMapper.insert(user);
    }

    @Override
    public String doLogin(User user) {
        String md5PassWord = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5PassWord);
        User UserDB = userMapper.selectOne(new QueryWrapper<>(user));
        if (UserDB == null) {
            return null;
        }
        String ticket = UUID.randomUUID().toString().replace("", "");//replace踢出uuid中的"-"
        UserDB.setPassword("123456你猜猜看");//对数据进行脱敏处理
        String userJSON = ObjectMapperUtil.toJSON(UserDB);
        jedisCluster.setex(ticket, 7*24*60*60, userJSON);
        return ticket;
    }

}
